﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BAL.EstRec;

namespace Nav3
{
    /// <summary>
    /// Lógica de interacción para TagCreateControl.xaml
    /// </summary>
    public partial class TagCreateControl : UserControl
    {
#region Members
        public long idElement;
        public bool isPublish;
        public bool isSend;
#endregion

#region Constructors
        public TagCreateControl()
        {
            InitializeComponent();

            this.idElement = 0;
            this.lblName.Content = "";
        }
#endregion Constructors

#region Event Implementation
        private void lblCerrar_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //Eliminar elemento de la base de datos
            var res=MessageBox.Show("¿Esta seguro de eliminar este elemento?", "Eliminar Elemento", MessageBoxButton.YesNoCancel);
            

            if (MessageBoxResult.Yes==res)
            {
                BgtVersion.DeleteBgtVersion(this.idElement);
                this.convertParent().Children.Remove(this);
            }
        }

        private void lblEditar_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //Cambiar a edicion
            this.RaiseClickEditEvent();
            this.changeToEditing();
        }

        private void txtName_KeyUp(object sender, KeyEventArgs e)
        {
            //Validar el enter
            if (e.Key == Key.Enter)
            {
                //Insertar en la base de datos
                BgtVersion version = new BgtVersion();
                version.name = this.txtName.Text;
                if (this.idElement <= 0)
                {
                    this.idElement = BgtVersion.InsertBgtVersion(version);
                }
                else
                {
                    //Actualizar
                    version.unid = this.idElement;
                    BgtVersion.UpdateBgtVersion(version);
                }

                this.lblName.Content = version.name;
                this.changeToRead();
                this.RaiseSaveItemEvent();
            }
            else
            {
                //Usuario, en modo de edicion, teclea "Escape"
                if (e.Key == Key.Escape)
                {
                    if (this.idElement <= 0)
                    {
                        //Quitar control de padre
                        ((StackPanel)this.Parent).Children.Remove(this);
                    }
                    else
                    {
                        //Ya existe elemento en la base de datos, solo se pasa a modo "Lectura"
                        this.changeToRead();
                    }
                }
                this.RaisePostEscapeEvent();
            }
        }

        private void lblName_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //Selección de item
            this.RaiseClickEvent();
        }
#endregion

#region Methods
        public void changeToEditing()
        {
            this.lblName.Visibility = Visibility.Collapsed;
            this.txtName.Text = this.lblName.Content.ToString();
            this.txtName.Visibility = Visibility.Visible;
            this.setFocusToTextBox(this);
            this.RaiseOnEditingEvent();
        }
        public void setFocusToTextBox(DependencyObject element)
        {
            FocusManager.SetFocusedElement(element, this.txtName);
            Keyboard.Focus(this.txtName);
        }
        public void changeToRead()
        {
            this.lblName.Visibility = Visibility.Visible;
            this.txtName.Visibility = Visibility.Collapsed;
        }

        public void setControlData(long idElement, string name, bool isPublish, bool isSend)
        {
            this.idElement = idElement;
            this.lblName.Content = name;
            this.isPublish = isPublish;
            this.isSend = isSend;
            this.changeToRead();
        }
        public StackPanel convertParent()
        {
            return (StackPanel)this.Parent;
        }
        public void setSelected()
        {
            this.dpMain.Background = Brushes.LightBlue;
        }
        public void setNoSelected()
        {
            this.dpMain.Background = Brushes.WhiteSmoke;
        }
#endregion

#region events definition
        //SaveItemEvent : Se acciona cuando se da enter (osea guardar uno nuevo o actualizar uno existente)
        public static readonly RoutedEvent SaveItemEvent = EventManager.RegisterRoutedEvent(
            "SaveItem", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TagCreateControl));

        public event RoutedEventHandler SaveItem
        {
            add { AddHandler(SaveItemEvent, value); }
            remove { RemoveHandler(SaveItemEvent, value); }
        }
        void RaiseSaveItemEvent()
        {
            RoutedEventArgs saveItemEventArgs = new RoutedEventArgs(TagCreateControl.SaveItemEvent);
            RaiseEvent(saveItemEventArgs);
        }

        //OnEditing : Se activa cuando el control entra a edicion
        public static readonly RoutedEvent OnEditingEvent = EventManager.RegisterRoutedEvent(
            "OnEditingEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TagCreateControl));

        public event RoutedEventHandler OnEditing
        {
            add { AddHandler(OnEditingEvent, value); }
            remove { RemoveHandler(OnEditingEvent, value); }
        }
        void RaiseOnEditingEvent()
        {
            RoutedEventArgs onEditingEventArgs = new RoutedEventArgs(TagCreateControl.OnEditingEvent);
            RaiseEvent(onEditingEventArgs);
        }

        //ClickEdit : Se dispara cuando se da click en editar
        public static readonly RoutedEvent ClickEditEvent = EventManager.RegisterRoutedEvent(
            "ClickEdit", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TagCreateControl));

        public event RoutedEventHandler ClickEdit
        {
            add { AddHandler(ClickEditEvent, value); }
            remove { RemoveHandler(ClickEditEvent, value); }
        }
        void RaiseClickEditEvent()
        {
            RoutedEventArgs clickEditEventArgs = new RoutedEventArgs(TagCreateControl.ClickEditEvent);
            RaiseEvent(clickEditEventArgs);
        }

        //Click : Se dispara cuando se da click en el caption (label)
        public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent(
            "Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TagCreateControl));

        public event RoutedEventHandler Click
        {
            add { AddHandler(ClickEvent, value); }
            remove { RemoveHandler(ClickEvent, value); }
        }
        public void RaiseClickEvent()//cambio
        {
            RoutedEventArgs clickEventArgs = new RoutedEventArgs(TagCreateControl.ClickEvent);
            RaiseEvent(clickEventArgs);
        }

        //PostEscape : Se dispara despues de que se ha dado escape
        public static readonly RoutedEvent PostEscapeEvent = EventManager.RegisterRoutedEvent(
            "PostEscape", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TagCreateControl));
         
        public event RoutedEventHandler PostEscape
        {
            add { AddHandler(PostEscapeEvent, value); }
            remove { RemoveHandler(PostEscapeEvent, value); }
        }
        void RaisePostEscapeEvent()
        {
            RoutedEventArgs postEscapeEventArgs = new RoutedEventArgs(TagCreateControl.PostEscapeEvent);
            RaiseEvent(postEscapeEventArgs);
        }
#endregion events definition

        private void dpMain_MouseMove(object sender, MouseEventArgs e)
        {
            this.dpMain.Background = Brushes.LightBlue;
        }
    }
}
